package com.un10.uploadPhotos;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.un10.util.BaseServlet;
import com.un10.util.Result;



@WebServlet("/ProductPhotosServlet")
public class ProductPhotosServlet extends BaseServlet {

	// ͼƬ�ϴ�
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Result res = null;
		Gson g = new Gson();
		PrintWriter out = response.getWriter();
	
		SmartUpload smart = new SmartUpload();
	
		smart.setCharset("utf-8");
	
		smart.initialize(getServletConfig(), request, response);
		try { 
			smart.upload();
		
			smart.save("/houtai/images");
		
			String filename = smart.getFiles().getFile(0).getFileName();
			res = new Result(true, 200, "上传成功", 1, filename);
			out.print(g.toJson(res));
		} catch (SmartUploadException e) {
			res = new Result(false, 200, "上传失败", 0, null);
			out.print(g.toJson(res));
			e.printStackTrace();
		}
		out.flush();
		out.close();
	}

}
